package cn.xiqi.cms.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.beans.CateBean;
import cn.xiqi.cms.hibernate.beans.Metadata;
import cn.xiqi.cms.oper.MetaOper;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MetaAction extends BaseAction {

	private static final long serialVersionUID = 3893718110481270974L;
	private JSON json;
	private String cate;
	private String key;
	private Integer value;
	private Integer dataID;
	private String oldCate;
	private String cateNames;

	public String cateList(){
		MetaOper oper = new MetaOper();
		List<String> metas = oper.cateList();
		Map<String, Object> map;
		
		map = formatCate(metas);
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String metaList(){
		MetaOper oper = new MetaOper();
		List<Metadata> metas = oper.metaList(cate);
		Map<String, Object> map = new HashMap<>();
		
		map.put("rows", metas);
		map.put("total", metas.size());
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String saveData(){
		MetaOper oper = new MetaOper();
		List<Metadata> metas;
		Map<String, Object> map = new HashMap<>();
		boolean b;
		String msg;
		
		if(dataID == 0){
			if(oper.isDataUnique(cate, value)){
					b = oper.addData(cate, key, value);
					msg = "";
			}else{
				b = false;
				msg = "同一个字典不能有重复的值";
			}
		}else{
			b = oper.updateData(dataID, key, value);
			msg = "";
		}
		if(b){
			map.put("success", true);
			metas = oper.metaList(cate);
			map.put("rows", metas);
			map.put("total", metas.size());
		}else{
			map.put("success", false);
			map.put("msg", msg);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String deleteData(){
		MetaOper oper = new MetaOper();
		List<Metadata> metas;
		Map<String, Object> map = new HashMap<>();
		boolean b;
		
		b = oper.deleteData(dataID, cate);
		if(b){
			map.put("success", true);
			metas = oper.metaList(cate);
			map.put("rows", metas);
			map.put("total", metas.size());
		}
		else{
			map.put("success", false);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String addCate(){
		MetaOper oper = new MetaOper();
		List<String> metas;
		Map<String, Object> map;
		boolean b = false;
		String msg;
		
		if(oper.isCateExist(cate)){
			b = false;
			msg = "数据字典已存在！";
		}else{
			b = oper.addCate(cate);
			if(b){
				msg = "保存成功";
			}else{
				msg = "保存失败";
			}
		}
		if(b){
			metas = oper.cateList();
			map = formatCate(metas);
			map.put("success", true);
			map.put("msg", msg);
		}else{
			map = new HashMap<>();
			map.put("success", false);
			map.put("msg", msg);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String editCate(){
		MetaOper oper = new MetaOper();
		List<String> metas;
		Map<String, Object> map;
		boolean b = false;
		String msg;
		
		if(oper.isCateExist(cate)){
			b = false;
			msg = "数据字典已存在！";
		}else{
			b = oper.editCate(oldCate, cate);
			if(b){
				msg = "保存成功";
			}else{
				msg = "保存失败";
			}
		}
		if(b){
			metas = oper.cateList();
			map = formatCate(metas);
			map.put("success", true);
			map.put("msg", msg);
		}else{
			map = new HashMap<>();
			map.put("success", false);
			map.put("msg", msg);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	public String deleteCate(){
		MetaOper oper = new MetaOper();
		List<String> metas;
		Map<String, Object> map;
		boolean b;
		
		b = oper.deleteCate(cate);
		if(b){
			metas = oper.cateList();
			map = formatCate(metas);
			map.put("success", true);
		}else{
			map = new HashMap<>();
			map.put("success", false);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String getCates(){
		MetaOper oper = new MetaOper();
		List<Metadata> metas;
		List<Map<String, Object>> datas;
		Map<String, Object> metamap;
		Map<String, Object> map = new HashMap<>();
		List<String> cates;
		
		cates = JSONArray.toList(JSONArray.fromObject(cateNames));
		for(String cateName: cates){
			datas = new ArrayList<>();
			metas = oper.metaList(cateName);
			for(Metadata meta:metas){
				metamap = new HashMap<>();
				metamap.put("value", meta.getValue());
				metamap.put("key", meta.getKey());
				datas.add(metamap);
			}
			map.put(cateName, datas);
		}
		json = JSONObject.fromObject(map);
		return SUCCESS;
	}
	
	private Map<String, Object> formatCate(List<String> metas){
		List<CateBean> rows = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		
		for(String meta:metas){
			rows.add(new CateBean(meta));
		}
		map.put("rows", rows);
		map.put("total", metas.size());
		return map;
	}

	public JSON getJson() {
		return json;
	}

	public void setJson(JSON json) {
		this.json = json;
	}

	public String getCate() {
		return cate;
	}

	public void setCate(String cate) {
		this.cate = cate;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Integer getValue() {
		return value;
	}

	public void setValue(Integer value) {
		this.value = value;
	}

	public Integer getDataID() {
		return dataID;
	}

	public void setDataID(Integer dataID) {
		this.dataID = dataID;
	}

	public String getOldCate() {
		return oldCate;
	}

	public void setOldCate(String oldCate) {
		this.oldCate = oldCate;
	}

	public String getCateNames() {
		return cateNames;
	}

	public void setCateNames(String cateNames) {
		this.cateNames = cateNames;
	}
}
